<?php
/**
 * Created by PhpStorm.
 * User: ouxiaobao
 * Date: 17/2/18
 * Time: 12:04
 */
namespace app\wxxcx\controller;

use think\Controller;
use Wxapp\Wechat\Auth;
use Wxapp\Wechat\User;
use \think\Validate;
use app\consultant\model\Consultant; //引入模型
class Index extends Controller
{
    private $app_id = "";
    private $app_secret = '';


    public function _initialize()
    {
        $this->WxxcxUser = model("WxxcxUser");
        $this->app_id = config("config_wxxcx.app_id");
        $this->app_secret = config("config_wxxcx.app_secret");
    }


    /**
     * 微信小程序的登录,并保存session
     * @return \think\response\Json
     */
    public function commit_login()
    {
        $code = input("param.code");
        if (empty($code)) {
            return json([
                "state" => 0,
                "msg" => "缺少code!"
            ]);
        }
        $auth = new Auth($this->app_id, $this->app_secret);
        try {
            $data = $auth->authorize($code);
            $open_id = $data['openid'];
            $session_key = $data['session_key'];


            //生成客户端的session
            $client_session = md5(rand(1, 9999) . time());

            //以客户端session的名字作为键名,open_id和session_key作为值,保存到服务器的session中
            session($client_session, $open_id . "|" . $session_key);

            return json([
                "state" => 1,
                "msg" => 1,
                "client_session" => $client_session,
                "session_id"=> session_id()     //返回session_id
            ]);
        } catch (Exception $e) {
            return json([
                "state" => 0,
                "msg" => "登录失败!"
            ]);
        }
    }

    /**
     * 保存用户信息
     * @return \think\response\Json
     */
    public function save_user_info()
    {
        $client_session = input("param.client_session");
        $encrypt_data = input("param.encrypt_data");
        $iv = input("param.iv");
        if (empty($client_session || empty($encrypt_data) || empty($iv))) {
            return json([
                "state" => 0,
                "msg" => "非法操作!"
            ]);
        }
        //获取回session_key
        $session = explode("|", session($client_session));
        $session_key = $session[1];

        if (empty($session_key)) {
            return json([
                "state" => 0,
                "msg" => "非法操作!"
            ]);
        }

        $user = new User($this->app_id, $this->app_secret);
        try {
            $info = $user->decode($session_key, $encrypt_data, $iv);
            $info = json_decode($info,true);
            $data = [
                "openid" => $info['openId'],
                "unionid" => isset($info['unionId'])?$info['unionId']:"",
                "nickname" => $info['nickName'],
                "gender" => $info['gender'],
                "city" => $info['city'],
                "province" => $info['province'],
                "country" => $info['country'],
                "avatarurl" => $info['avatarUrl'],
                "last_time" => time(),
            ];
            $res = $this->WxxcxUser->where(["openid"=>$info['openId']])->find();
            if(empty($res)){//执行插入操作
                $this->WxxcxUser->save($data);
            }else{//执行更新操作
                $this->WxxcxUser->where(["openid"=>$info['openId']])->update($data);
            }


            return json([
                "state" => 1,
                "msg" => "数据操作成功!"
            ]);



        } catch (Exception $e) {
            return json([
                "state" => 0,
                "msg" => "获取用户信息失败!"
            ]);
        }
    }

    /*
     * 根据ID修改咨询者信息或添加咨询者信息
     * save(),success(),error()
     */
    public function commit_add_edit()
{
    $this->Consultant = model('consultant/Consultant');
    $is_wechat = input("param.is_wechat") == "true" ? true : false;
    $id = input("param.id");
    $head_img = trim(input("param.file_val"));
    $name = trim(input("param.name"));
    $sex = trim(input("param.sex"));
    $birthday = trim(input("param.birthday"));
    $education_id = trim(input("param.education_id"));
    $school = trim(input("param.school"));
    $isMarry = trim(input("param.isMarry"));
    $phone = trim(input("param.phone"));
    $wechat = trim(input("param.wechat"));
    $qq = trim(input("param.qq"));
    $address = trim(input("param.address"));
    $position = trim(input("param.position"));
    $money = trim(input("param.money"));
    $try_work_date = trim(input("param.try_work_date"));
    $evaluation = trim(input("param.evaluation"));
    $company_impression = trim(input("param.company_impression"));
    $msg = "";
    if(empty($name)==1){
        $msg='姓名不能为空';
    }else if(empty($birthday)==1){
        $msg='出生日期不能为空';
    }else if(empty($school)==1){
        $msg='毕业学校不能为空';
    }else if(empty($phone)==1){
        $msg='手机号码不能为空';
    }else if(empty($wechat)==1){
        $msg='微信号码不能为空';
    }else if(empty($qq)==1){
        $msg='QQ不能为空';
    }else if(empty($address)==1){
        $msg='居住地不能为空';
    }else if(empty($money)==1){
        $msg='期望工资不能为空';
    }else if(empty($try_work_date)==1){
        $msg='入职时间不能为空';
    }else if(empty($evaluation)==1){
        $msg='自我评价不能为空';
    }else if(empty($company_impression)==1){
        $msg='对蓝景的印象不能为空';
    }else if(is_date($birthday)==0||Validate::is($birthday,'date')==0){
        $msg='出生日期格式错误';
    }else if(is_phone($phone)==0){
        $msg='手机号码格式错误';
    }else if(is_num($qq)==0){
        $msg='QQ号码格式错误';
    }else if(is_num($money)==0){
        $msg='期望工资格式错误';
    }else if(is_date($try_work_date)==0||Validate::is($try_work_date,'date')==0){
        $msg='入职时间格式错误';
    }
    if($msg!=""){
        $return=[
            "status"=>0
            ,'msg'=>$msg
        ];
        return $return;
    }
    $info = [
        'head_img' => $head_img
        ,'name' => $name
        ,'sex' => $sex
        ,'birthday' => $birthday
        ,'education_id' => $education_id
        ,'school' => $school
        ,'isMarry' => $isMarry
        ,'phone' => $phone
        ,'wechat' => $wechat
        ,'qq' => $qq
        ,'address' => $address
        ,'position' => $position
        ,'money' => $money
        ,'try_work_date' => $try_work_date
        ,'evaluation' => $evaluation
        ,'company_impression' => $company_impression
        ,'is_wechat' => $is_wechat
    ];
    $this->Consultant->data($info);
    $res = $this->Consultant->save();
    if ($res) {
        $msg="添加成功";
    } else {
        $msg="添加失败";
    }
    $return=[
        "status"=>1
        ,'msg'=>$msg
    ];
    return $return;
}


}